package com.liu.common.exception;

import lombok.Getter;
import org.apache.http.HttpStatus;

/**
 * 自定义异常
 *
 * @author Mark sunlightcs@gmail.com
 */
@Getter
public class RRException extends RuntimeException {
	private static final long serialVersionUID = 1L;
	
    private String msg;
    private int code = 500;
    
    public RRException(String msg) {
		super(msg);
		this.msg = msg;
	}
	
	public RRException(String msg, Throwable e) {
		super(msg, e);
		this.msg = msg;
	}
	
	public RRException(String msg, int code) {
		super(msg);
		this.msg = msg;
		this.code = code;
	}
	
	public RRException(String msg, int code, Throwable e) {
		super(msg, e);
		this.msg = msg;
		this.code = code;
	}

	public static RRException newInstance(IErrorCode errorCode) {
		if (errorCode == null) {
			throw new RRException("内部异常，请联系管理员", HttpStatus.SC_INTERNAL_SERVER_ERROR);
		}

		throw new RRException(errorCode.getMsg(), errorCode.getCode());
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public void setCode(int code) {
		this.code = code;
	}
}
